package com.ddw.meedeeuh.views;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

import com.ddw.meedeeuh.presenters.EditorContainerPresenter.EditorContainerView;
import com.ddw.meedeeuh.presenters.MainPresenter.MainView;
import com.ddw.meedeeuh.presenters.MoviesPresenter.MoviesView;
import com.ddw.meedeeuh.presenters.TvShowsPresenter.TvShowsView;

@SuppressWarnings("serial")
public class MainViewImpl extends JFrame implements MainView {

	private static final Dimension MIN_SIZE = new Dimension(640, 480);
	private final JPanel moviesPanel;
	private final JPanel tvShowsPanel;
	private final JPanel editorPanel;

	public MainViewImpl() {
		 // Set the defaults
		setTitle("Meedeeuh");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setDefaultLookAndFeelDecorated(true);

		// Create file menu
		final JMenuBar menuBar = new JMenuBar();
		final JMenu menuFile = new JMenu("File");
		final JMenu menuFileExport = new JMenu("Export Database...");
		final JMenuItem menuFileExportMovies = new JMenuItem("Movies");
		final JMenuItem menuFileExportTvShows = new JMenuItem("TV Shows");
		menuFileExport.add(menuFileExportMovies);
		menuFileExport.add(menuFileExportTvShows);
		menuFile.add(menuFileExport);
		final JMenuItem menuFileExit = new JMenuItem("Exit");
		menuFile.add(menuFileExit);
		menuBar.add(menuFile);
		final JMenu menuHelp = new JMenu("Help");
		final JMenuItem menuHelpAbout = new JMenuItem("About");
		menuHelp.add(menuHelpAbout);
		menuBar.add(menuHelp);
		setJMenuBar(menuBar);

		// Create containers
		this.moviesPanel = new JPanel(new GridBagLayout());
		this.tvShowsPanel = new JPanel(new GridBagLayout());

		// Setup the left hand tabbed pane
		final JTabbedPane leftTabbedPane = new JTabbedPane(JTabbedPane.BOTTOM, JTabbedPane.SCROLL_TAB_LAYOUT);
		leftTabbedPane.addTab("Movies", moviesPanel);
		leftTabbedPane.addTab("TV Shows", tvShowsPanel);
		leftTabbedPane.setUI(new BasicTabbedPaneUI() {

			@Override
			protected void installDefaults() {
				super.installDefaults();

				this.tabInsets = new Insets(2, 5, 2, 5);
				this.contentBorderInsets = new Insets(1, 1, 1, 1);
			}

		});

		// Setup the editor pane
		this.editorPanel = new JPanel(new GridBagLayout());

		// Setup the splitter
		final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, leftTabbedPane, editorPanel);
		splitPane.setDividerLocation(new Integer(250));
		getContentPane().add(splitPane);

		// Set a preferred size and pack the window to fit
		setPreferredSize(new Dimension(800, 600));
		setMinimumSize(MIN_SIZE);
		addComponentListener(new ComponentAdapter() {

			@Override
			public void componentResized(ComponentEvent e) {
				int w = getWidth();
				int h = getHeight();
				boolean override = false;
				if(w < MIN_SIZE.width) {
					w = MIN_SIZE.width;
					override = true;
				}
				if(h < MIN_SIZE.height) {
					h = MIN_SIZE.height;
					override = true;
				}
				if(override) {
					setSize(w, h);
				}
			}

		});
		pack();
	}

	@Override
	public void showView() {
		setVisible(true);
	}

	@Override
	public void setMovies(MoviesView movies) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;

		moviesPanel.removeAll();
		moviesPanel.add(movies.asComponent(), gbc);
	}

	@Override
	public void setTvShows(TvShowsView tvShows) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;

		tvShowsPanel.removeAll();
		tvShowsPanel.add(tvShows.asComponent(), gbc);
	}

	@Override
	public Component asComponent() {
		return this;
	}

	@Override
	public void setEditor(EditorContainerView editorContainer) {
		final GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;

		editorPanel.removeAll();
		editorPanel.add(editorContainer.asComponent(), gbc);
	}

}
